package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author ck
 * @date 2021/11/11 11:19
 */

//暴露端口
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }

    @Override
    public void updateWithPhone(User user) {
        userMapper.updateById(user);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id", userIds);
        //2、添加筛选条件
        if (info != null) {
            if (info.getAge() != null) {
                qw.lt("age", info.getAge());
            }
            if (!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender", info.getGender());
            }
            if (!StringUtils.isEmpty(info.getNickname())) {
                qw.like("nickname", info.getNickname());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    @Override
    public IPage findAll(Integer page, Integer pagesize) {
        IPage<UserInfo> iPage = new Page<>(page, pagesize);
        return userInfoMapper.selectPage(iPage, null);
    }

    @Override
    public Map<Long, UserInfo> findList(List<Long> ids) {
//        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(UserInfo::getId, ids);
//        List<UserInfo> list = userInfoMapper.selectBatchIds(ids);
//        if (CollUtil.isEmpty(ids)) {
//            UserInfo userInfo = userInfoMapper.selectById(1);
//            Map<Long, UserInfo> map = new HashMap<>();
//            map.put(1L, userInfo);
//            return map;
//        }
        List<UserInfo> list = userInfoMapper.selectBatchIds(ids);
        Map<Long, UserInfo> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(ids.get(i), list.get(i));
        }
        return map;
    }
}